package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.foundations.OpationApi;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.Result;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {

    @Resource
    private AgencyCertificationMapper agencyCertificationMapper;


    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    public Result<?> agencyCertification(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        // TODO 保存机构认证信息
        // 调用mapper.insert()方法保存机构认证信息
        // 保存成功后返回机构认证信息
        AgencyCertification agencyCertification = new AgencyCertification();
        agencyCertification.setBusinessLicense(agencyCertificationAuditAddReqDTO.getBusinessLicense());// 营业执照
        agencyCertification.setLegalPersonIdCardNo(agencyCertificationAuditAddReqDTO.getLegalPersonIdCardNo());// 法人身份证号
        agencyCertification.setId(agencyCertificationAuditAddReqDTO.getServeProviderId()); // 机构id
        agencyCertification.setIdNumber(agencyCertificationAuditAddReqDTO.getIdNumber());//统一社会信用代码
        agencyCertification.setLegalPersonName(agencyCertificationAuditAddReqDTO.getLegalPersonName());// 法人姓名
        agencyCertification.setName(agencyCertificationAuditAddReqDTO.getName());// 企业名称
        int insert = baseMapper.insert(agencyCertification);
        String message = "";
        if (insert > 0) {
            message = "提交成功";
        }
        if (insert <= 0) {
            message = "提交失败";
        }
        return new Result(200, message, (Object) null);
    }


    @Autowired
    private OpationApi opationApi;


    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageList(AgencyCertificationAuditPageQueryReqDTO agencyCertificationAuditPageQueryReqDTO) {
//        AgencyCertificationAuditResDTO agencyCertificationAuditResDTO = new AgencyCertificationAuditResDTO();
//
//        Long id = UserContext.getCurrentUser().getId();
//        System.out.println(id);
//        Operator operator = opationApi.queryById(id);
//
//        System.out.println(operator.getName());

//            Operator operator=  opeartionService.queryById(id);
        // 出错了
//        agencyCertificationAuditResDTO.setAuditorId(operator.getId());
//        agencyCertificationAuditResDTO.setAuditorName(operator.getName());
//        System.out.println(agencyCertificationAuditResDTO.toString());
//
//        // 调用 baseMapper.queryListss 获取数据
        // 给我把、auditor_ids和auditor_names、修改数据表agency_certification的内容、然后修改的是




        PageResult<AgencyCertificationAuditResDTO> agencyCertificationAuditResDTOPageResult = PageHelperUtils.selectPage(agencyCertificationAuditPageQueryReqDTO,
                () -> baseMapper.queryListss(agencyCertificationAuditPageQueryReqDTO));

        return agencyCertificationAuditResDTOPageResult;
    }



}
